﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSQuaTrinhCongTacDang : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public void EditQuaTrinhCongTacDang(int hdId, DateTime ngayNhanXet, string noiDungNhanXet, string chiboDang)
        {
            List<NSQuaTrinhCongTacDang> lst = (List<NSQuaTrinhCongTacDang>)HttpContext.Current.Session["QuaTrinhCongTacDang"];
            NSQuaTrinhCongTacDang pc = lst.Where(a => a.QuaTrinhCongTacDangID.Equals(hdId)).SingleOrDefault();
            pc.NgayNhanXet = ngayNhanXet;
            pc.NoiDungNhanXet = noiDungNhanXet;
            pc.ChiBoDang = chiboDang;

        }
        public void DeleteQTCTDang(int hdId)
        {
            List<NSQuaTrinhCongTacDang> lst = (List<NSQuaTrinhCongTacDang>)HttpContext.Current.Session["QuaTrinhCongTacDang"];
            NSQuaTrinhCongTacDang pc = lst.Where(a => a.QuaTrinhCongTacDangID.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllQTCTDangByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSQuaTrinhCongTacDangs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSQuaTrinhCongTacDangs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateQTCTDang_NhanVien(int nhanVienId, List<NSQuaTrinhCongTacDang> obj)
        {

            var queryNBLByNhanVienId = (from a in db.NSQuaTrinhCongTacDangs
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSQuaTrinhCongTacDangs.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSQuaTrinhCongTacDang pc in obj)
            {
                NSQuaTrinhCongTacDang item = new NSQuaTrinhCongTacDang();
                CopyNSQuaTrinhCongTacDang(pc, item);
                db.NSQuaTrinhCongTacDangs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSQuaTrinhCongTacDang(NSQuaTrinhCongTacDang src, NSQuaTrinhCongTacDang des)
        {
            des.NgayNhanXet = src.NgayNhanXet;
            des.NhanVienID = src.NhanVienID;
            des.NoiDungNhanXet = src.NoiDungNhanXet;
            des.ChiBoDang = src.ChiBoDang;
        }
        public List<NSQuaTrinhCongTacDang> LoadQTCTDangByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSQuaTrinhCongTacDangs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.QuaTrinhCongTacDangID descending
                         select a).ToList();
            return query;

        }
        public NSQuaTrinhCongTacDang GetQTCTDang(int nhanVienId, DateTime ngayNhanXet, string chiboDang, string noiDungNhanXet)
        {
            var db = new DataClassesDataContext();
            NSQuaTrinhCongTacDang hd = new NSQuaTrinhCongTacDang();
            hd.QuaTrinhCongTacDangID = 0 - DateTime.Now.Second;
            if (!ngayNhanXet.Year.Equals(0001))
            {
                hd.NgayNhanXet = ngayNhanXet;
            }
            hd.NoiDungNhanXet = noiDungNhanXet;
            hd.ChiBoDang = chiboDang;
            hd.NhanVienID = nhanVienId;

            return hd;
        }
    }
}